bitmasks brute force *800

Please click on ads to support us..

Python Code:

x = int(input())
  
for jj in range(x):
    n = int(input())
    if n == 1:print(3)
    else:
        if n%2 == 1:
            print(1)
        else:
            p = 0
            k = n
            while n%2==0:
                p += 1
                n //= 2
            t = 2**p
            if t == k:
                print(t+1)
            else:
                print(t)

C++ Code:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        if (n % 2)
    { // odd
        if (n > 1)
            cout << "1\n";
        else
            cout << "3\n";
    }
    else
    {
        if (!(n & (n - 1)))
        { // power of two
            cout << n + 1 << endl;
        }
        else
        { // we are leaving the most sig. bit and unsetting all bits.
            cout << (n & ~(n - 1)) << endl;
        }
    }
    }
}


Comments

Submit
0 Comments
More Questions

1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes